import math, random, asyncio
import logging

log = logging.getLogger(__name__)

x: int = 0  # grad
N = 360.0
count = 0
saw = 1
lift = 0

async def gen_sin_distortion():
    global x, count, saw, lift
    try:
        while count < N * 100000:
            if (random.random() < 0.5):    
                saw = int(5 + random.random() * 30)
            if (random.random() < 0.05): 
                lift = random.random() * 300
            t = int(x / saw) * saw
            r = { 'y': int(120 + 50 * math.sin(t * 4/ N * math.pi * 2)),
                  'x': int(150 + 150 * math.cos(t / N * math.pi * 2)),
                  'count': count }

            # log.debug("%s sin %s", count, '*' * int(r["y"]/3))
            x = (x + 1) % N if saw < 1000 else x
            count += 1            
            yield r
            await asyncio.sleep(1/30);  # 30 fps

    except Exception as ex:
        log.error(ex, exc_info=True)
    log.info('sin END')


async def gen_sin():
    global x, count
    while count < 10000:
        r = { 'y': int(math.sin(x / N * math.pi * 2) * 100 + 200), 'x': x % 100 }
        x += 1
        count += 1
        yield r
        await asyncio.sleep(1/30);  # 30 fps
